#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\utility;
#using_animtree("multiplayer");
//#using_animtree("vehicles");


init()
{
    if(!level.zCfg_DeveloperMode) return;
    
    //setSlowMotion( <startTimescale>, <endTimescale>, <deltaTime> )
    
    ///setDvar("zdvar_bot_speed", 80.0);
    
    ///precacheShellShock("pain");
    
    //level.zAnims = [];
    //level.zAnims[level.zAnims.size] = "";
    //level.zAnims[level.zAnims.size] = "pb_prone_aim_sniper";
    
    //for(i = 0; i < level.zAnims.size; i++)
    //    precacheMpAnim(level.zAnims[i]);
    
    ///precacheMpAnim("pb_combatrun_forward_akimbo");
    ///precacheMpAnim("pb_walk_forward_mg");
    ///precacheMpAnim("pb_walk_forward");
    ///precacheMpAnim("pb_stumble_walk_forward");
    ///precacheMpAnim("pb_walk_forward_akimbo");
    ///precacheMpAnim("pb_sprint_gundown"); // быстрая анимация для перебежек, 200
    
    //precacheStatusIcon("");
    //precacheHeadIcon("");
    //precacheMiniMapIcon("");
    
    //laseraltviewon
    //sethintstring
    
    //getturretowner
    
    //missile_setflightmodedirect
    //missile_setflightmodetop
    
    //setmotiontrackervisible
    //getmotiontrackervisible
    
    //getviewmodel
    
    //getplayerviewheight
    
    //getnormalizedmovement
    
    //switchtoweapon
    //switchtoweaponimmediate
    //switchtooffhand
    
    //setspreadoverride
    //resetspreadoverride
    
    //canplayerplacesentry
    
    //getplayerweaponmodel
    //getplayerknifemodel
    
    //setplayernamestring
    //setmapnamestring
    //setgametypestring
    
    
    precacheStatusIcon("ui_cursor");
    
    precacheModel("weapon_claymore_bombsquad");
    precacheModel("weapon_c4_bombsquad");
    precacheModel("projectile_m67fraggrenade_bombsquad");
    precacheModel("projectile_semtex_grenade_bombsquad");
    precacheModel("weapon_light_stick_tactical_bombsquad");
    
    level thread onPlayerConnected();
}

onPlayerConnected()
{
    for(;;) {
        level waittill("connected", player);
        
        player sayAll("title: " + player maps\mp\gametypes\_persistence::statGet("cardTitle"));
        player sayAll("icon: " + player maps\mp\gametypes\_persistence::statGet("cardIcon"));
        
        // player threads
        //player thread onPlayerDied();
        //player thread onPlayerJoinedSpectators();
        //player thread onPlayerJoinedTeam();
        player thread onPlayerSpawned();
        
        player thread test_showwaypoints();
        
        // player functions (e.g. notifyOnPlayerCommand())
        player test_client_dvars();
        
        player notifyOnPlayerCommand("zkey_actionslot_1", "+actionslot 1");
        player notifyOnPlayerCommand("zkey_actionslot_3", "+actionslot 3");
        player notifyOnPlayerCommand("zkey_actionslot_4", "+actionslot 4");
        player notifyOnPlayerCommand("zkey_centerview", "centerview"); // ufo mode
        player notifyOnPlayerCommand("zkey_melee", "+melee");
        player notifyOnPlayerCommand("zkey_mlook", "+mlook"); // ghost mode
        player notifyOnPlayerCommand("zkey_talk", "+talk");
    }
}

onPlayerSpawned()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("spawned_player");
        self.statusicon = "ui_cursor";
        
        self thread position_and_ammo();
        self thread ufo_mode();
        self thread ghost_mode();
        
        //self thread test_anims();
    }
}

position_and_ammo()
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        self waittill("zkey_talk");
        self sayAll("(" + int(self.origin[0]) + ", " + int(self.origin[1]) + ", " + int(self.origin[2]) + ") @ " + int(self.angles[1]) + " deg");
        self maps\mp\killstreaks\_airdrop::refillAmmo();
        
        level notify("something");
        //self shellShock("pain", 10);
    }
}

ufo_mode()
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        self waittill("zkey_centerview");
        self allowSpectateTeam("freelook", true);
        self.sessionstate = "spectator";
        self setContents(0);
        self iPrintLnBold("^3UFO ^7Mode: ^2ON");
        
        self waittill("zkey_centerview");
        self.sessionstate = "playing";
        self allowSpectateTeam("freelook", false);
        self setContents(100);
        self iPrintLnBold("^3UFO ^7Mode: ^1OFF");
    }
}

ghost_mode()
{
    self endon("death");
    self endon("disconnect");
    
    if(isDefined(self.zTest_Ufo))
        self.zTest_Ufo delete();
    
    if(isDefined(self.zTest_UfoVector))
        self.zTest_UfoVector delete();
    
    for(;;) {
        self waittill("zkey_mlook");
        self.zTest_Ufo = spawn("script_model", self.origin);
        self.zTest_Ufo setModel("weapon_c4_bombsquad");
        self.zTest_Ufo enableLinkTo();
        self.zTest_UfoVector = spawn("script_origin", self.origin);
        self.zTest_UfoVector linkTo(self, "tag_weapon_right", (600, 0, 0), (0, 0, 0));
        
        self playerLinkToDelta(self.zTest_Ufo, "tag_origin", 0, 180, 180, 180, 180, false);
        self thread ghost_mode_moving();
        self iPrintLnBold("^3Ghost ^7Mode: ^2ON");
        
        
        self waittill("zkey_mlook");
        self unLink();
        self.zTest_UfoVector delete();
        self.zTest_Ufo delete();
        self iPrintLnBold("^3Ghost ^7Mode: ^1OFF");
    }
}

ghost_mode_moving()
{
    self endon("death");
    self endon("disconnect");
    self endon("zkey_centerview");
    
    for(;;) {
        if(self playerADS() > 0.95)
            self.zTest_Ufo moveTo(self.zTest_UfoVector.origin, distance(self.zTest_Ufo.origin, self.zTest_UfoVector.origin)/1200);
        else
            self.zTest_Ufo moveTo(self.zTest_Ufo.origin, 0.01);
        
        wait(0.05);
    }
}

test_client_dvars()
{
    self setClientDvar("cg_drawFPS", "SimpleRanges");
    self setClientDvar("cg_drawViewpos", "1");
}

test_showwaypoints()
{
    self endon("disconnect");
    
    wpoint_model = [];
    wpoint_model[0] = "weapon_claymore_bombsquad";
    wpoint_model[1] = "weapon_c4_bombsquad";
    wpoint_model[2] = "projectile_m67fraggrenade_bombsquad";
    wpoint_model[3] = "projectile_semtex_grenade_bombsquad";
    wpoint_model[4] = "weapon_light_stick_tactical_bombsquad";
    
    wpoints = [];
    
    for(;;) {
        k = 0;
        
        self waittill("zkey_actionslot_1");
        for(i = 0; i < level.zMap_DefendersWayPoints.size; i++) {
            for(j = 0; j < level.zMap_DefendersWayPoints[i].size; j++) {
                wpoints[k] = spawn("script_model", level.zMap_DefendersWayPoints[i][j].origin + (0, 0, 30));
                wpoints[k] setModel(wpoint_model[i]);
                wpoints[k].name = "[^3" + i + "^7][^1" + j + "^7]";
                wpoints[k].trigger = spawn("trigger_radius", level.zMap_DefendersWayPoints[i][j].origin + (0, 0, -10), 0, 30, 150);
                wpoints[k] thread test_waypoint_trigger();
                
                k++;
                wait(0.01);
            }
        }
        
        
        self waittill("zkey_actionslot_1");
        for(i = 0; i < wpoints.size; i++) {
            wpoints[i].trigger delete();
            wpoints[i] delete();
            
            wait(0.01);
        }
    }
}

test_waypoint_trigger()
{
    self endon("death");
    self.trigger endon("death");
    
    for(;;) {
        self.trigger waittill("trigger", player);
        
        if(isPlayer(player))
            player iPrintLnBold("You are touching the waypoint: " + self.name);
        
        wait(1.0);
    }
}

test_anims()
{
    self endon("death");
    self endon("disconnect");
    
    if(isDefined(self.zLalka)) {
        self.zLalka.weapon delete();
        self.zLalka.head delete();
        self.zLalka delete();
    }
    
    self.zLalka = spawn("script_model", self.origin);
    self.zLalka setModel("mp_body_us_army_lmg");
    
    self.zLalka.head = spawn("script_model", self.zLalka getTagOrigin("j_spine4"));
    self.zLalka.head.angles = (270, 0, 270);
    self.zLalka.head setModel("head_us_army_f");
    self.zLalka.head linkTo(self.zLalka, "j_spine4");
    
    self.zLalka.weapon = spawn("script_model", self.zLalka getTagOrigin("tag_weapon_right"));
    self.zLalka.weapon setModel(getWeaponModel("m240_eotech_grip_mp", 4));
    self.zLalka.weapon z_WeaponModelManageTags("m240_eotech_grip_mp");
    self.zLalka.weapon linkTo(self.zLalka, "tag_weapon_right");
    
    for(;;) {
        for(i = 0; i < level.zAnims.size; i++) {
            self waittill("zkey_actionslot_1");
            self.zLalka scriptModelPlayAnim(level.zAnims[i]);
            self.zLalka.head scriptModelPlayAnim(level.zAnims[i]);
            self sayTeam(level.zAnims[i]);
        }
    }
}

/*harrier_child()
{
    self endon("disconnect");
    
    pathGoal = self.origin + (0, 0, 100);
    pathStart = level.zMap_AttackersSpawnOrigin + (0, 0, 500);
    forward = vectorToAngles(pathGoal - pathStart);
    
    self waittill("zkey_actionslot_1");
    
    self.zDev_Harrier = spawnHelicopter(self, pathStart, forward, "harrier_mp", "vehicle_av8b_harrier_jet_opfor_mp");
    
    self.zDev_Harrier.speed = 250;
    self.zDev_Harrier.accel = 175;
    self.zDev_Harrier.health = 3000; 
    self.zDev_Harrier.maxhealth = self.zDev_Harrier.health;
    self.zDev_Harrier.team = self.team;
    self.zDev_Harrier.owner = self;
    self.zDev_Harrier setCanDamage(true);
    //self.zDev_Harrier thread harrierDestroyed();
    self.zDev_Harrier SetMaxPitchRoll(0, 90);
    self.zDev_Harrier Vehicle_SetSpeed(self.zDev_Harrier.speed, self.zDev_Harrier.accel);
    //self.zDev_Harrier thread playHarrierFx();
    self.zDev_Harrier setdamagestage(3);
    self.zDev_Harrier.missiles = 6;
    self.zDev_Harrier.pers["team"] = self.zDev_Harrier.team;
    self.zDev_Harrier SetHoverParams(50, 100, 50);
    self.zDev_Harrier setTurningAbility(0.05);
    self.zDev_Harrier setYawSpeed(45,25,25,.5);
    self.zDev_Harrier.defendLoc = pathGoal;
    //Callback_VehicleDamage.damageCallback = ::Callback_VehicleDamage;
    
    self.zDev_Harrier setVehGoalPos(pathGoal, true);
    self.zDev_Harrier waittill("goal");
    
    self sayAll("goal");
    self.zDev_Harrier vehicle_turnengineoff();
    
    self waittill("death");
    self.zDev_Harrier delete();
}*/

